<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/30
 * Time: 17:20
 */
namespace App\Http\Controllers\Api\V2;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\EntMobileCode;
use App\Modules\EntMemberInfo;
use App\Modules\EntBookHots;
use Validator, DB;
use App\Common\MobileMassege;


class MemberInfoController extends BaseController
{
    /**
     * 签名方法
     * @var string
     */
    protected $sign_method = 'md5';
    protected $sign_name = 'laohoulundao';
    /**
     * 错误码
     * @var [type]
     */
    public static $errCodes = [
        // 系统码
        '200' => '成功',
        '400' => '未知错误',
        '401' => '无此权限',
        '500' => '服务器异常',

        // 公共错误码
        '1001' => '',
        '1002' => '',
        '1003' => '',
        '1004' => '',
        '1005' => '',
        '1006' => '[sign]缺失',
        '1007' => '[sign]签名错误',
        '1010' => '[nonce]缺失',
        '1011' => '[nonce]必须为字符串',
        '1012' => '[nonce]长度必须为1-32位',
    ];





    /**
     * 校验签名
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    protected function checkSign($params)
    {
        $sign = array_key_exists('sign', $params) ? $params['sign'] : '';

        if (empty($sign)) {
            return array('status' => false, 'code' => '1006');
        }
        unset($params['sign']);
        if ($sign != $this->generateSign($params)) {
            return array('status' => false, 'code' => '1007');
        }
        return array('status' => true, 'code' => '200');
    }

    /**
     * 生成签名
     * @param  array $params 待校验签名参数
     * @return string|false
     */
    protected function generateSign($params)
    {
        if ($this->sign_method == 'md5') {
            $t = $this->generateMd5Sign($params);

            return $t;
        }

        return false;
    }

    /**
     * md5方式签名
     * @param  array $params 待签名参数
     * @return string
     */
    protected function generateMd5Sign($params)
    {
        ksort($params);
        $tmps = array();
        foreach ($params as $k => $v) {
            $tmps[] = $k . $v;
        }
        $string = implode('', $tmps) . $this->sign_name;

        return strtoupper(md5($string));
    }

    /**
     * 随机数字
     * @param $n 随机数的位数
     * @return string
     */
    protected function randStr($n)
    {
        $s = '';
        $str = "1238856789"; // 输出字符集
        $len = strlen($str) - 1;
        for ($i = 0; $i < $n; $i++) {
            $s .= $str[rand(0, $len)];
        }
        return $s;
    }

}